<template>
  <div>
    <van-list>
      <van-cell>
        <h4>发表评论</h4>
    <hr/>
    <van-field v-model="message" autosize type="textarea" placeholder="请输入留言" size="large" />
    <van-button type="info" @click="method" class="btn1">发表评论</van-button>
        <!-- 遍历获取过来的数据  -->
        <div v-for="(item,index) in list" :key="item.id">
          <div class="title">
            <span>第{{index +1}}楼&nbsp;</span>
            <span>用户：{{item.user_name}}&nbsp;</span>
            <span>发表时间：{{item.add_time | dateFormat('')}}&nbsp;</span>
          </div>
          <!-- 评论内容 -->
          <div class="content">
            <span>{{item.content}}</span>
          </div>
          <!-- 加载组件 -->
          <div class="loading">
            <van-loading size="24px">加载中...</van-loading>
          </div>
        </div>
        <!-- 底部的加载按钮 -->
        <van-button color="#F67C8A" plain @click="getNewList" class="btn2">加载按钮</van-button>
      </van-cell>
    </van-list>
  </div>
</template>
<script>
export default {
  data() {
    return {
      //textarea中的数据
      message: '',
      // 获取到的数据
      list: [],
      // 第二页的评论数据
      num: 2,
    }
  },
  created() {
    this.getList()
    // this.getList1()
  },
  methods: {
    //    async getList1(){
    //           const res= await this.$http.get('/api/getnewslist')
    //           console.log(res);
    //  },
    //获取第一页的评论列表
    async getList() {
      const { data: res } = await this.$http.get(
        // this.$route.params.id
        '/api/getcomments/'+this.$route.params.id+'?pageindex=1'
      )
      this.list = res.message
      console.log(this.list)
    },
    //点击加载后更新的评论
    async getNewList() {
      const { data: res } = await this.$http.get(
        '/api/getcomments/'+this.$route.params.id+'?pageindex=' + this.num
      )
      // 获取的评论的数量不为空时获取下一页的数据，当数据为空时返回信息
      if(res.message.length !==0){
      console.log(res)
      this.list.push(...res.message)
      this.num++
      }else{
        this.$toast('没有更多数据')
      }
      
    },
    //发布评论
    async method() {
      // 判断内容非空
      if(this.message.trim().length === 0){
        this.$toast('输入的值不能为空')
      }
      // console.log(111)
      const { data: res } = await this.$http.post('/api/postcomment/13', {
        params: { content: this.message }
      })
      console.log(res)    
    }
  }
}
</script>
<style scoped>
.van-field {
  height: 100px;
  border: 1px solid #ccc;
}
.btn1{
  width: 100%;
  margin-top: 10px !important;
}
.btn2{
  width: 100%;
  margin-top: 10px!important;
  margin-bottom: 50px !important;
}

.title {
  height: 30px;
  background-color: #ccc;
  font-size: 12px;
  line-height: 30px;
}
.content {
  height: 50px;
  font-size: 12px;
}
.loading {
  text-align: center;
}

</style>